package com.tanhua.admin.service;

import cn.hutool.cache.CacheUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.utils.Constants;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ManagerService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private MovementApi movementApi;
    @DubboReference
    private VideoApi videoApi;
    @Autowired
    private RedisTemplate redisTemplate;

    public PageResult findAllUsers(Integer page, Integer pagesize) {
        IPage iPage = userInfoApi.findAll(page,pagesize);
        List<UserInfo> list = iPage.getRecords();
        for (UserInfo userInfo : list) {
            String key = Constants.USER_FREEZE + userInfo.getId();
            if(redisTemplate.hasKey(key)) {
                userInfo.setUserStatus("2");
            }
        }
        return new PageResult(page,pagesize,iPage.getTotal(),iPage.getRecords());
    }

    public UserInfo findUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        String key = Constants.USER_FREEZE + userId;
        if(redisTemplate.hasKey(key)) {
            userInfo.setUserStatus("2");
        }
        return userInfo;
    }

    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        PageResult result = movementApi.findByUserId(uid,state,page,pagesize);
        List<Movement> items = (List<Movement>) result.getItems();
        if (CollUtil.isEmpty(items)){
            return new PageResult();
        }
        List<Long> userId = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findbyIds(userId, null);
        List<MovementsVo> movementsVoList = new ArrayList<>();
        for (Movement item : items) {
            UserInfo userInfo = map.get(item.getUserId());
            if (userInfo!=null){
                MovementsVo movementsVo = MovementsVo.init(userInfo, item);
                movementsVoList.add(movementsVo);
            }
        }
        result.setItems(movementsVoList);
        return result;
    }

    public PageResult findAllVideos(Integer page, Integer pagesize, Long uid) {

        return videoApi.findbyids(page,pagesize,uid);
    }

    public Map userFreeze(Map params) {
        String userId = params.get("userId").toString();
        String key = Constants.USER_FREEZE+userId;
        Integer freezingTime = Integer.valueOf(params.get("freezingTime").toString());
        int days = 0;
        if (freezingTime == 1) {
            days = 3;
        }
        if (freezingTime == 2) {
            days = 7;
        }
        if (freezingTime == 3) {
            days = -1;
        }
        String value = JSON.toJSONString(params);
        if (days>0){
            redisTemplate.opsForValue().set(Constants.USER_FREEZE+userId,value,days, TimeUnit.MINUTES);
        }else {
            redisTemplate.opsForValue().set(key,value);
        }
        Map map = new HashMap();
        map.put("message","冻结成功");
        return map;
    }

    public Map userUnfreeze(Map params) {
        String userId = params.get("userId").toString();
        String key = Constants.USER_FREEZE + userId;
        redisTemplate.delete(key);
        Map retMap = new HashMap();
        retMap.put("message","解冻成功");
        return retMap;
    }
}
